package com.geeguo.ebuilder.operation.controller.system.webmodule;

import com.geeguo.ebuilder.core.common.action.ActionResult;
import com.geeguo.ebuilder.core.common.utils.key.UUIDCheckUtils;
import com.geeguo.ebuilder.core.common.utils.key.UUIDGenerator;
import com.geeguo.ebuilder.core.security.annotation.RequiresPermissions;
import com.geeguo.ebuilder.operation.base.core.constants.MessageConstants;
import com.geeguo.ebuilder.operation.base.core.constants.VirtualConstants;
import com.geeguo.ebuilder.operation.base.core.dictionary.ClientTypeEnum;
import com.geeguo.ebuilder.operation.base.core.dictionary.LogTypeEnum;
import com.geeguo.ebuilder.operation.base.security.model.LoginUser;
import com.geeguo.ebuilder.operation.base.security.utils.AuthUtils;
import com.geeguo.ebuilder.operation.base.log.annotation.Log;
import com.geeguo.ebuilder.operation.business.system.webmodule.model.*;
import com.geeguo.ebuilder.operation.business.system.webmodule.service.WebModuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "Web端菜单信息")
@RestController
public class WebModuleController {

    @Autowired
    private WebModuleService webModuleService;

    @Operation(summary = "查询树形菜单列表")
    @PostMapping("/system/webModule/listAllTree")
    @RequiresPermissions("System:WebModule")
    public ActionResult<List<WebModuleVO>> listAllTree() {
        return ActionResult.success(webModuleService.listAllTree());
    }

    @Operation(summary = "保存菜单")
    @PostMapping("/system/webModule/save")
    @RequiresPermissions("System:WebModule")
    public ActionResult<Boolean> save(@Validated @RequestBody WebModuleCreateDTO dto) {
        LoginUser currentUser = AuthUtils.getLoginUser();
        WebModuleEntity entity = WebModuleConverter.INSTANCE.convert2Entity(dto);
        if (webModuleService.existsName(entity.getName())) {
            return ActionResult.fail(MessageConstants.EXIST_NAME);
        }
        entity.setId(UUIDGenerator.getId());
        if (!UUIDCheckUtils.check(entity.getParentId())) {
            entity.setParentId(VirtualConstants.PARENT_ID);
            entity.setDepth(1);
        } else {
            entity.setDepth(webModuleService.get(entity.getParentId()).getDepth() + 1);
        }
        if (dto.getSequence() == null) {
            entity.setSequence(webModuleService.getMaxSequence(entity.getParentId()) + 1);
        }
        entity.setCreateTime(new Date());
        entity.setCreateUser(currentUser.getUserId());
        if (webModuleService.save(entity)) {
            return ActionResult.success();
        }
        return ActionResult.fail();
    }

    @Operation(summary = "根据ID获取信息")
    @GetMapping("/system/webModule/get/{id}")
    @RequiresPermissions("System:WebModule")
    public ActionResult<WebModuleVO> get(@PathVariable("id") String id) {
        if (UUIDCheckUtils.check(id)) {
            WebModuleVO module = webModuleService.get(id);
            if (module != null) {
                return ActionResult.success(module);
            }
            return ActionResult.fail(MessageConstants.FAIL_DATA_NOT_EXIST);
        }
        return ActionResult.fail(MessageConstants.FAIL_REQUEST_DATA_ERROR);
    }

    @Operation(summary = "更新菜单")
    @PostMapping("/system/webModule/update")
    @RequiresPermissions("System:WebModule")
    public ActionResult<Boolean> update(@Validated @RequestBody WebModuleUpdateDTO dto) {
        LoginUser currentUser = AuthUtils.getLoginUser();
        WebModuleVO old = webModuleService.get(dto.getId());
        if (old != null) {
            WebModuleEntity entity = WebModuleConverter.INSTANCE.convert2Entity(dto);
            if (!entity.getName().equals(old.getName())) {
                if (webModuleService.existsName(entity.getName())) {
                    return ActionResult.fail(MessageConstants.EXIST_NAME);
                }
            }
            entity.setModifyTime(new Date());
            entity.setModifyUser(currentUser.getUserId());
            if (webModuleService.update(entity, WebModuleConverter.INSTANCE.convert2Entity(old))) {
                return ActionResult.success();
            }
        }
        return ActionResult.fail();
    }

    @Operation(summary = "删除菜单")
    @PostMapping("/system/webModule/remove")
    @RequiresPermissions("System:WebModule")
    @Log(title = "删除Web端菜单", logType = LogTypeEnum.DELETE, clientType = ClientTypeEnum.Web)
    public ActionResult<Boolean> remove(@RequestParam("id") String id) {
        if (UUIDCheckUtils.check(id)) {
            if (webModuleService.existsChild(id)) {
                return ActionResult.fail(MessageConstants.EXIST_CHILD);
            }
            if (webModuleService.remove(id)) {
                return ActionResult.success();
            }
        }
        return ActionResult.fail();
    }

    @Operation(summary = "根据ID获取菜单路径")
    @GetMapping("/system/webModule/getFullPath/{id}")
    @RequiresPermissions("System:WebModule")
    public ActionResult<String> getFullPath(@PathVariable("id") String id) {
        if (UUIDCheckUtils.check(id)) {
            return ActionResult.success(webModuleService.getFullPath(id));
        }
        return ActionResult.fail(MessageConstants.FAIL_REQUEST_DATA_ERROR);
    }
}